package controller;

import Pojo.Applyout;
import Pojo.User;
import Pojo.Userlist;
import Pojo.Zulist;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.ApplyoutService;
import service.UserlistService;
import service.ZulistService;

@Controller
@RequestMapping({"/applyout"})
public class ApplyoutController
{

  @Autowired
  private ZulistService zulistService;

  @Autowired
  private ApplyoutService applyoutService;

  @Autowired
  private UserlistService userlistService;

  @RequestMapping({"/insertapplyout"})
  public String insertapplyout(String house_id, Model model)
  {
    Zulist zulist = this.zulistService.findzulist(house_id);
    this.applyoutService.insertapplyout(zulist);
    model.addAttribute("error", "applysuccess");
    return "redirect:/zulist/myzulist.action";
  }

  @RequestMapping({"/findallapplyout"})
  public String findallapplyout(Model model, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List applyout = this.applyoutService.findallapplyout();
    PageInfo p = new PageInfo(applyout);
    model.addAttribute("applyout", applyout);
    model.addAttribute("p", p);
    model.addAttribute("mainPage", "applyout.jsp");
    return "admin/main1";
  }

  @RequestMapping({"/refuseapplyout"})
  public String refuseapplyout(Model model, Integer id) {
    Applyout applyout = new Applyout();
    applyout.setId(id);
    applyout.setStatus("已拒绝");
    this.applyoutService.updateapplyout(applyout);
    model.addAttribute("mainPage", "applyout.jsp");
    return "redirect:findallapplyout.action";
  }

  @RequestMapping({"/agreeapplyout"})
  public String agreeapplyout(Model model, Integer id) {
    this.applyoutService.agreeapplyout(id);
    model.addAttribute("error", "applyoutsucess");
    return "redirect:findallapplyout.action";
  }

  @RequestMapping({"/deleteapplyout"})
  public String deleteapplyout(Model model, Integer id) {
    this.applyoutService.deleteapplyout(id);
    model.addAttribute("error", "deletesucess");
    return "redirect:findallapplyout.action";
  }

  @RequestMapping({"/getmyapplyout"})
  public String getmyapplyout(Model model, HttpSession httpSession, @RequestParam(required=false, defaultValue="1") Integer page, @RequestParam(required=false, defaultValue="5") Integer pageSize)
  {
    User user1 = (User)httpSession.getAttribute("user");
    Userlist userlist = this.userlistService.findhasuserlist(user1.getId());
    PageHelper.startPage(page.intValue(), pageSize.intValue());
    List list = this.userlistService.getmyapplyout(userlist.getId());
    PageInfo p = new PageInfo(list);
    model.addAttribute("userlist", list);
    model.addAttribute("p", p);
    model.addAttribute("mainPage", "myapplyout.jsp");
    return "zuke/main";
  }
}